#!/bin/bash

echo "=== 网络诊断脚本 ==="
echo

echo "1. 网络接口信息:"
ip addr show
echo

echo "2. 路由表信息:"
ip route show
echo

echo "3. DNS配置:"
cat /etc/resolv.conf
echo

echo "4. 测试本地回环:"
ping -c 2 127.0.0.1
echo

echo "5. 测试网关连通性:"
GATEWAY=$(ip route | grep default | awk '{print $3}' | head -1)
if [ -n "$GATEWAY" ]; then
    echo "网关地址: $GATEWAY"
    ping -c 3 $GATEWAY
else
    echo "未找到默认网关"
fi
echo

echo "6. 测试DNS服务器:"
ping -c 2 8.8.8.8
echo

echo "7. 测试目标主机:"
ping -c 3 222.175.245.230
echo

echo "8. 网络接口状态:"
for iface in $(ip link show | grep -E '^[0-9]+:' | cut -d: -f2 | tr -d ' '); do
    echo "接口 $iface:"
    ethtool $iface 2>/dev/null | grep -E "(Link detected|Speed|Duplex)" || echo "  无法获取详细信息"
done
echo

echo "9. ARP表:"
arp -a
echo

echo "诊断完成!"